package com.sultan.guante.userSystem.web;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.naming.AuthenticationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sultan.guante.userSystem.service.SecurityService;





	@ManagedBean(name = "loginBean")
	@SessionScoped
	@Component
	public class LoginBean implements Serializable {
	private static final long serialVersionUID = 1L;
	 

	private String login;
	
	private String password;
	 

	@ManagedProperty(value = "#{securityService}")
	private SecurityService securityService; // injected Spring defined service for bikes
	
	 
	
	 
	
	public String getLogin() {
		return login;
	}





	public void setLogin(String login) {
		this.login = login;
	}





	public String getPassword() {
		return password;
	}





	public void setPassword(String password) {
		this.password = password;
	}





	public SecurityService getSecurityService() {
		return securityService;
	}





	public void setSecurityService(SecurityService securityService) {
		this.securityService = securityService;
	}





	public String login() throws AuthenticationException {
	 

	boolean success = false;
	success = securityService.login(login, password);
	 
	if (success){

	return "index.xhtml"; // return to application but being logged now
	
	}
	
	else{
	
	FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Login or password incorrect."));  
	
	return "accessDenied.xhtml";
	
	}
	
	}
	
	}

